package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxSystemGroupData;

import co.yixiang.hw.repository.YxSystemGroupDataRepository;
import co.yixiang.hw.service.YxSystemGroupDataService;
import co.yixiang.hw.service.dto.YxSystemGroupDataDto;
import co.yixiang.hw.service.dto.YxSystemGroupDataQueryCriteria;
import co.yixiang.hw.service.mapper.YxSystemGroupDataMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-02-12
*/
@Service
@CacheConfig(cacheNames = "yxSystemGroupData")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxSystemGroupDataServiceImpl implements YxSystemGroupDataService {

    @Resource
    private YxSystemGroupDataRepository yxSystemGroupDataRepository;
    @Resource
    private YxSystemGroupDataMapper yxSystemGroupDataMapper;

    @Override
    @Cacheable(value = "index::routine_home_banner")
    public Object getIndexBannerList() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("routine_home_banner");
        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }


    //routine_home_menus
    @Override
    @Cacheable(value = "index::routine_home_menus")
    public Object getIndexHomeMenusList() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("routine_home_menus");
        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    @Override
    @Cacheable(value = "index::time_limit_discount")
    public Object getTimeLimitDiscount() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("time_limit_discount");
        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    /**
     * 本周爆款
     *
     * @return
     */
    @Override
    @Cacheable(value = "index::index_week_hot")
    public Object getWeekHot() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("index_week_hot");
        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    /**
     * 往期推荐
     *
     * @return
     */
    @Override
    @Cacheable(value = "index::history_recommend")
    public Object getHistoryRecommend() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("history_recommend");

        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    @Override
    @Cacheable(value = "index::cate_product")
    public Object getCateGoods() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("cate_product");

        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    @Override
    @Cacheable(value = "index::index_title")
    public Object getIndexTitle() {
        YxSystemGroupDataQueryCriteria criteria = new YxSystemGroupDataQueryCriteria();
        criteria.setGroupName("index_title");
        List<YxSystemGroupDataDto> list = this.queryAll(criteria);
        return this.parseData(list);
    }

    private Object parseData(List<YxSystemGroupDataDto> list){
        JSONArray result = new JSONArray();
        if(list != null && list.size() > 0){
            List<YxSystemGroupDataDto> collect = list.stream().sorted(Comparator.comparing(YxSystemGroupDataDto::getSort)).collect(Collectors.toList());
            for (YxSystemGroupDataDto dto : collect) {
                JSONObject jsonObject = JSONObject.parseObject(dto.getValue());
                result.add(jsonObject);
            }
        }
        return result;
    }

    @Override
//    @Cacheable
    public List<YxSystemGroupDataDto> queryAll(YxSystemGroupDataQueryCriteria criteria){
        return yxSystemGroupDataMapper.toDto(yxSystemGroupDataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxSystemGroupDataDto findById(Integer id) {
        YxSystemGroupData yxSystemGroupData = yxSystemGroupDataRepository.findById(id).orElseGet(YxSystemGroupData::new);
        ValidationUtil.isNull(yxSystemGroupData.getId(),"YxSystemGroupData","id",id);
        return yxSystemGroupDataMapper.toDto(yxSystemGroupData);
    }
}
